package com.xzk.controller;

import com.xzk.config.FileDfsUtil;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Classname FileController
 * @Description TODO
 * @Date 2020/10/20 11:34
 * @Created by ccc-j
 * @email ccc-ju@outlook.com
 */
@Api("文件上传")
@RestController
public class FileController {

    @Autowired
    private FileDfsUtil fileDfsUtil;

    /**
     * 上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST, headers = "content-type=multipart/form-data")
    public ResponseEntity<String> uploadFile(@RequestParam("file")MultipartFile file){
        String result = "";
        try {
            String path = fileDfsUtil.upload(file);   //group0//M00/00/00/xxxx.jpg
            if(StringUtils.isEmpty(path)){
                result = "上传失败";
            }else{
                result = path;
            }
        } catch (IOException e) {
            e.printStackTrace();
            result = "服务器异常!";
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 删除文件
     * @param filePathName "/group1/M00/00/00/xxx.jpg"
     * @return
     */
    @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
    public ResponseEntity<String> deleteByPath(String filePathName){

        //删除大图
        fileDfsUtil.deleteFile(filePathName);

        return ResponseEntity.ok("success delete");

    }



}
